package com.teacold.customdemo.chapter_seven;

import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.View;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.teacold.customdemo.R;
import com.teacold.utils.BitmapToRound_Util;

public class TFRadarView extends View {
    private Context mContext;
    private Paint mPaint, mPaint2;
    private Path mPath;
    private float animValue;
    private String imageUrl;
    private Bitmap centerBitmap;

    public void setImageUrl(String imageUrl) {
        Glide.with(mContext.getApplicationContext()).load(imageUrl).asBitmap().into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                centerBitmap = new BitmapDrawable(new BitmapToRound_Util().toRoundBitmap(resource)).getBitmap();
                invalidate();
            }
        }); //方法中设置
    }

    public TFRadarView(Context context) {
        this(context, null);
    }

    public TFRadarView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TFRadarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        mPaint = new Paint();
        mPaint2 = new Paint();
        centerBitmap=BitmapFactory.decodeResource(getResources(), R.mipmap.menu_seven);

        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, 1f);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                animValue = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        valueAnimator.setDuration(800);
        valueAnimator.start();
    }


    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setColor(Color.RED);
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() * 3 / 8 * animValue, mPaint);
        mPaint.setColor(Color.BLUE);
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 8 + getWidth() * animValue / 8, mPaint);
        canvas.drawBitmap(centerBitmap,null,new RectF(0,0,100,100),mPaint);

    }
}
